const router = require('koa-router')()
const mysql = require('mysql');

router.prefix('/community')

// 创建数据库链接
let connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'root',
    password: '123456789',
    database: 'duxing'
})

const { query } = require("../public/javascripts/db")

router.post('/content', async(ctx, next) => {
    console.log("出发了");
    console.log(ctx.request.body);
    let sql = "select * from community"
    let sql1 = "select * from user where user_id = ?"
    let results;
    await query(sql, []).then(async (res) => {
        console.log(12456)
        console.log(res);
        results = res;
        for(let i = 0 ; i < res.length ; i++) {
            await query(sql1, [results[i]['user_id']]).then((res) => {
                results[i]['src'] = res[0]['src'];
                results[i]['user_name'] = res[0]['user_name'];
                console.log(results[i]["src"]);
                console.log("789456123");
            })
            console.log("zhegsadiowaion");
        }
        console.log(res);
        console.log("zhege");
        ctx.body = res;
        
    }) 
})

router.post('/givelike', async(ctx , next) => {
    console.log(ctx.request.body);

    let sql = "update community set giveLikeCount = ? where user_id = ? AND time = ?";
    query(sql , [JSON.stringify(ctx.request.body.giveLikePeople), ctx.request.body.openid, ctx.request.body.time]).then((res) => {
        console.log(res);
    })

    ctx.body = "success";
})

router.post('/comment', async(ctx , next) => {
    console.log(123456);
    console.log(ctx.request.body);

    let sql = "update community set commentContent = ? where user_id = ? AND time = ?";
    query(sql , [JSON.stringify(ctx.request.body.commentContent), ctx.request.body.openid, ctx.request.body.time]).then((res) => {
        console.log(res);
    })
    
    ctx.body = "success";
})

router.post("/username", async(ctx, next) => {
    let sql = "select user_name from user where user_id = ?";
    query(sql, [ctx.request.body.openid]).then(res => {
        console.log(res);
        ctx.body = "success"
    })
})

router.post("/storage", (ctx, next) => {
    let obj = ctx.request.body;
    let object = {};
    console.log(ctx.request.body);
    let arr = obj.detail.split('<p>');
    for(let i = 1 ; i < arr.length; i++) {
        console.log();
        if(arr[i].indexOf('<img') != -1) {
            console.log(arr[i].slice(arr[i].indexOf("http"), arr[i].lastIndexOf("\"")));
            object.image = arr[i].slice(arr[i].indexOf("http"), arr[i].lastIndexOf("\""));
        } else {
            console.log("consdw");
            if(arr[i].indexOf("<br>") != -1) {
                object.text = ''
            } else {
                object.text = arr[i].slice(0 , arr[i].indexOf("</p>"))
            }
            

        }
    }
    object.time = obj.time;
    object.openid = obj.openid;
    console.log(object);
    console.log(arr);
    console.log(arr[1].indexOf("http"));
    console.log(arr[1].lastIndexOf("\""));
    console.log(arr[1].slice(arr[1].indexOf("http"), arr[1].lastIndexOf("\"")));


    let sql = "insert into community(image, text, user_id, giveLikeCount, commentCount, time, completedTarget, commentContent) value(?, ?, ?, ?, ?, ?, ?, ?)"
    

    query(sql , [object.image, object.text, object.openid, "[]", 0 , object.time, 15, "[]"]).then(res => {
        console.log(res);
    })
    
    ctx.body = "success"

})

module.exports = router